package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.entity.User;
import com.our.set.service.CommonService;
import com.our.set.service.HzService;
import com.our.sys.project1.RunConstants;
import com.our.sys.project1.dao.FreshLogsDao;
import com.our.sys.project1.dao.StockDao;
import com.our.sys.project1.dao.StockDetailDao;
import com.our.sys.project1.dao.StockInDao;
import com.our.sys.project1.entity.FreshLogs;
import com.our.sys.project1.entity.Stock;
import com.our.sys.project1.entity.StockDetail;
import com.our.sys.project1.entity.StockIn;

@Service
@Transactional
public class StockService {

    private static Logger logger = LoggerFactory.getLogger(StockService.class);

    @Autowired
    private StockInDao stockInDao;
    @Autowired
    private StockDetailDao stockDetailDao;
    @Autowired
    private CommonService commonService;
    @Autowired
    private FreshLogsDao freshLogsDao;
    @Autowired
    private StockDao stockDao;
    @Autowired
    private HzService hzService;

    @Transactional(readOnly = true)
    public Page<StockIn> queryStockIn(Page<StockIn> page, List<PropertyFilter> filters) {
        Page<StockIn> curPage = stockInDao.findPage(page, filters);
        return curPage;
    }
    
    @Transactional(readOnly = true)
    public Stock queryById(Long id) {
        return stockDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = stockDao.findUnique("select count(a) from Stock a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }
    
    @Transactional(readOnly = true)
    public Page<Stock> queryStock(Page<Stock> page, List<PropertyFilter> filters) {
        Page<Stock> curPage = stockDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Page<StockDetail> queryStockDetail(Page<StockDetail> page, List<PropertyFilter> filters) {
        Page<StockDetail> curPage = stockDetailDao.findPage(page, filters);
        return curPage;
    }

    public void validation(Long id) throws Exception {
        StockIn si = stockInDao.findOneBy("id", id);
        try {
            si.setState(RunConstants.STOCK_CHECK);
            stockInDao.save(si);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void fresh(Long id,User u) throws Exception {
        StockIn si = stockInDao.findOneBy("id", id);
        try {
            si.setState(RunConstants.STOCK_FRESH);
            stockInDao.save(si);
            
            FreshLogs fl = new FreshLogs();
            fl.setMoney(si.getAllMoney());
            fl.setNumber(si.getNumber());
            fl.setStockNo(si.getStockNo());
            fl.setTime(commonService.getNowString());
            fl.setWho(u.getName());
            freshLogsDao.save(fl);
            
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
    
    public void save(Stock stock) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(stock.getName());
            stock.setPy(map.get("py"));
            stock.setWb(map.get("wb"));
            if (null != stock.getId()) {
                Stock getStock = this.queryById(stock.getId());

                if (queryCountByName(getStock.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个仓库！");
                }

                getStock.setName(stock.getName());
                getStock.setState(stock.getState());
                getStock.setPy(stock.getPy());
                getStock.setWb(stock.getWb());
                stockDao.save(getStock);
            } else {

                if (queryCountByName(stock.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个仓库！");
                }

                stockDao.save(stock);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            stockDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
